package ua.kiev.tosik.price.modules.clients.models;

import org.apache.commons.lang.time.DateUtils;
import ua.kiev.tosik.price.gui.models.SimpleTableModel;
import ua.kiev.tosik.price.models.Clients;
import ua.kiev.tosik.price.modules.clients.conf.ModuleConfig;

import javax.swing.*;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class ClientsTableModel extends SimpleTableModel {

	private List<Clients> data;
	private ImageIcon clientIcon;
	private ModuleConfig config;

	private ImageIcon upToDate;
	private ImageIcon expiredToDate;
	private ImageIcon failToDate;

	private Date currentDate;

	public ClientsTableModel(List<Clients> data) {
		this.data = data;
		config = ModuleConfig.getInstance();
		clientIcon = new ImageIcon(config.get("clients.table.icon"));
		colNames = new String[]{
			"[..]",
			config.get("clients.table.name"),
			config.get("clients.table.phone"),
			config.get("clients.table.address"),
			config.get("clients.table.status")
		};

		upToDate = new ImageIcon(config.get("clients.table.status.upToDate.icon"));
		expiredToDate = new ImageIcon(config.get("clients.table.status.expiredToDate.icon"));
		failToDate = new ImageIcon(config.get("clients.table.status.failToDate.icon"));
		currentDate = new Date();
	}

	@Override
	public int getRowCount() {
		return data.size();
	}

	public void setColNames(String[] cols) {
		this.colNames = cols;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Clients clients = data.get(rowIndex);
		ImageIcon icon = null;
		if ( clients.getLastPriceDate() != null ) {
			int dateRange = (int)(DateUtils.getFragmentInDays(currentDate, Calendar.YEAR)-DateUtils.getFragmentInDays(clients.getLastPriceDate(), Calendar.YEAR));
			if ( dateRange <= 0 ) {
				icon = upToDate;
			} else {
				if ( dateRange == 3 ) {
					icon = expiredToDate;
				} else {
					icon = failToDate;
				}
			}
		}
		switch (columnIndex) {
			case 0:
				return clientIcon;
			case 1:
				return clients.getName();
			case 2:
				return clients.getPhone();
			case 3:
				return clients.getAddress();
			case 4:
				return icon;
			default:
				return "";
		}
	}

	@Override
	public Class getColumnClass(int c) {
		switch (c) {
			case 0:
				return (ImageIcon.class);
			case 4:
				return (ImageIcon.class);
			default:
				return (String.class);
		}
	}

	public void removeRow(int idx) {
		data.remove(idx);
		fireTableRowsDeleted(idx, idx);
	}

	public void addRowSet(List<Clients> data) {
		this.data.clear();
		this.data.addAll(data);
		fireTableDataChanged();
		logger.debug("Added " + this.data.size() + " records to table");
	}

	public void addRow(Clients rowData) {
		this.data.add(rowData);
		fireTableDataChanged();
	}

	public void addRow(int rowIndex, Clients rowData) {
		this.data.add(rowIndex, rowData);
		fireTableRowsInserted(rowIndex, rowIndex);
	}

	public void updateRow(int rowIndex, Clients rowData) {
		this.data.set(rowIndex, rowData);
		fireTableRowsUpdated(rowIndex, rowIndex);
	}

	public Clients getRow(int index) {
		return data.get(index);
	}

}
